package com.xingyang.service.impl;


import com.xingyang.controller.request.AccountLoginRequest;
import com.xingyang.controller.request.AccountRegisterRequest;
import com.xingyang.enums.AuthTypeEnum;
import com.xingyang.enums.BizCodeEnum;
import com.xingyang.enums.SendCodeEnum;
import com.xingyang.manager.AccountManager;
import com.xingyang.model.AccountDO;
import com.xingyang.model.LoginUser;
import com.xingyang.service.AccountService;

import com.xingyang.service.NotifyService;
import com.xingyang.utils.CommonUtil;
import com.xingyang.utils.IDUtil;
import com.xingyang.utils.JWTUtil;
import com.xingyang.utils.JsonData;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 信仰
 * @since 2023-11-23
 */
@Service
@Slf4j
public class AccountServiceImpl  implements AccountService {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private AccountManager accountManager;

// * 手机验证码验证
//* 密码加密（TODO）
// *账号唯一性检查(TODO)
//* 插入数据库
//* 新注册用户福利发放(TODO)
    @Override
    public JsonData register(AccountRegisterRequest registerRequest) {

        boolean checkCode=false;

        if (StringUtil.isNotBlank(registerRequest.getPhone())) {
         checkCode=   notifyService.checkCode(SendCodeEnum.USER_REGISTER,registerRequest.getPhone(),registerRequest.getCode());

        }
        //验证码错误
        if (!checkCode){
            return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }

        AccountDO accountDO=new AccountDO();
        BeanUtils.copyProperties(registerRequest,accountDO);

        //认证级别
        accountDO.setAuth(AuthTypeEnum.DEFAULT.name());

        //生成唯一的账号   TODO
       // accountDO.setAccountNo(CommonUtil.getCurrentTimestamp());
        accountDO.setAccountNo((Long) IDUtil.geneSnowFlakeID());

        //设置密码
        accountDO.setSecret("$1$"+ CommonUtil.getStringNumRandom(8));

        //加密密码
        String cryptPwd= Md5Crypt.md5Crypt(registerRequest.getPwd().getBytes(),accountDO.getSecret());

        accountDO.setPwd(cryptPwd);
        int rows = accountManager.insert(accountDO);

        log.info("rows{},注册成功",rows);

        //用户注册成功发福利
        userRegisterInitTask(accountDO);
        return JsonData.buildSuccess();
    }

    //用户注册成功发福利 流量包 TODO
    private void userRegisterInitTask(AccountDO accountDO) {


    }


    /*
    * 1.根据手机号去找
    * */
    @Override
    public JsonData login(AccountLoginRequest request) {
        List<AccountDO> accountDoList = accountManager.findByPhone(request.getPhone());
        if (accountDoList!=null&&accountDoList.size()==1){

        AccountDO accountDO=  accountDoList.get(0);

            String md5Crypt = Md5Crypt.md5Crypt(request.getPwd().getBytes(), accountDO.getSecret());
            if (md5Crypt.equalsIgnoreCase(accountDO.getPwd())){
                

                LoginUser loginUser = LoginUser.builder().build();

                String token = JWTUtil.geneJsonWebToken(loginUser);

                BeanUtils.copyProperties(accountDO,loginUser);

                return JsonData.buildSuccess(token);
            }else{
                return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);

            }

        }else {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_UNREGISTER);
        }

    }


}
